var t = require;
var e = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0});
var a = (function () {
    function t() {
        this.allWeight = {};
        this.rule1WeightList = {};
        this.starW = 0.4;
        this.checkList = [];
        this.centerValue = 0.5;
        this.maxWeight = 1;
        this.tidu = 0.01;
        this.cenShu = 3;
        this.weightCount = 5;
        this.learnCount = 1e4;
        this.bodyListObj = {};
        this.rightCount = 0;
        this.errorCount = 0;
    }
    t.prototype.checkLearnThread = function (t) {
        var e = this;
        this.loadData();
        for (var o = 0; o < 1; o++) this.learnAi([1, 3, 5, 7], !0);
        0 == this.checkIsAi([1, 2, 3, 4], !0) ? this.rightCount++ : this.errorCount++;
        1 == this.checkIsAi([1, 3, 5, 7], !0) ? this.rightCount++ : this.errorCount++;
        console.log(
            "AiBaseBean ----------------- 正确率:" +
                Math.floor((1e4 * this.rightCount) / (this.rightCount + this.errorCount)) / 1e4
        );
        this.saveData();
        setTimeout(function () {
            if (e.learnCount > 0) {
                e.learnCount--;
                e.checkLearnThread(t);
            }
        }, 100);
    };
    t.prototype.learnAi = function (t, e) {
        if (this.allWeight && !this.allWeight.data) {
            this.allWeight.data = {};
            this.checkAddWeight(t.length);
        }
        if (this.rule1WeightList && !this.rule1WeightList.data) {
            this.rule1WeightList.data = {};
            this.checkRuleCount(t.length);
        }
        for (var o = 0; o < this.cenShu + 1; o++)
            for (var a = 0; a < this.weightCount; a++) this.rule1WeightList[o][a] = 0;
        for (o = 0; o < t.length; o++) this.rule1WeightList[0][o] = t[o];
        for (o = 0; o < this.cenShu; o++)
            this.rule1WeightList[o + 1] = this.onceWeight(this.rule1WeightList[o], this.allWeight[o], t.length);
        var i = this.getObjAveAddCount(this.rule1WeightList[this.cenShu], this.centerValue, t.length),
            n = this.getOverCount(this.rule1WeightList[this.cenShu], t.length);
        for (a = 0; a < this.weightCount; a++) this.rule1WeightList[this.cenShu][a] = i;
        for (o = this.cenShu; o > 0; o--) {
            var r = this.checkSel(
                this.rule1WeightList[o - 1],
                this.allWeight[o - 1],
                t.length,
                i,
                n > this.centerValue != e
            );
            this.allWeight[o - 1] = r.w;
            i = r.rew;
        }
    };
    t.prototype.checkSel = function (t, e, o, a, i) {
        var n = this.getOffectWidObj(e, a),
            r = this.getOffectWidObj(e, -a),
            s = this.onceWeight(t, n, o),
            c = this.onceWeight(t, r, o),
            p = this.getObjAveAddCount(s, this.centerValue, o),
            l = this.getObjAveAddCount(c, this.centerValue, o),
            d = i ? Math.min(a, p, l) : Math.max(a, p, l);
        return d == p ? {w: n, rew: d} : d == l ? {w: r, rew: d} : {w: e, rew: d};
    };
    t.prototype.getOffectWidObj = function (t, e) {
        var o = {};
        for (var a in t) {
            o[a] = [];
            for (var i in t[a]) o[a].push(t[a][i] + e);
        }
        return o;
    };
    t.prototype.onceWeight = function (t, e, o) {
        for (var a = {}, i = 0; i < this.weightCount; i++) a[i] = 0;
        for (i = 0; i < o; i++)
            for (var n = 0; n < this.weightCount; n++) {
                var r = t[i] * e[i][n];
                a[n] += r;
            }
        return a;
    };
    t.prototype.getObjAveAddCount = function (t, e, o) {
        for (var a = 0, i = 0; i < o; i++) {
            var n = this.getEddata(t[i]);
            a += Math.pow(n - e, 2);
        }
        return a / (2 * o);
    };
    t.prototype.getOverCount = function (t, e) {
        for (var o = 0, a = 0; a < e; a++) o += t[a];
        return this.getEddata(o / e);
    };
    t.prototype.getList = function (t, e) {
        for (var o = [], a = 0; a < e; a++) o.push(t[a]);
        return o;
    };
    t.prototype.checkAddWeight = function (t) {
        for (var e = this.cenShu, o = 0; o < e; o++) this.allWeight[o] = this.checkAddObjWeight({}, t);
    };
    t.prototype.checkRuleCount = function (t) {
        for (var e = this.cenShu + 1, o = 0; o < e; o++) {
            for (var a = {}, i = 0; i < t; i++) a[i] = 0;
            this.rule1WeightList[o] = a;
        }
    };
    t.prototype.checkAddObjWeight = function (t, e) {
        t || (t = {});
        for (var o = 0; o < e; o++) {
            t[o] = [];
            for (var a = 0; a < this.weightCount; a++) t[o].push(this.getRand(this.maxWeight));
        }
        return t;
    };
    t.prototype.checkIsAi = function (t, e) {
        void 0 === e && (e = !1);
        return this.baseAiObj(t) > this.centerValue;
    };
    t.prototype.getEddata = function (t) {
        return (Math.sin(t) + 1) / 2;
    };
    t.prototype.resetetEddata = function (t) {
        return Math.log(t);
    };
    t.prototype.checkbaseAiObj = function (t) {
        for (var e = this.cenShu + 1, o = 0; o < e; o++) {
            for (var a = {}, i = 0; i < t; i++) a[i] = 0;
            this.bodyListObj[o] = a;
        }
    };
    t.prototype.baseAiObj = function (t) {
        if (this.allWeight && !this.allWeight.data) {
            this.allWeight.data = {};
            this.checkAddWeight(t.length);
        }
        if (this.bodyListObj && !this.bodyListObj.data) {
            this.bodyListObj.data = {};
            this.checkbaseAiObj(t.length);
        }
        for (var e = 0; e < this.cenShu + 1; e++) for (var o = 0; o < this.weightCount; o++) this.bodyListObj[e][o] = 0;
        for (e = 0; e < t.length; e++) this.bodyListObj[0][e] = t[e];
        for (e = 0; e < this.cenShu; e++)
            this.bodyListObj[e + 1] = this.onceWeight(this.bodyListObj[e], this.allWeight[e], t.length);
        return this.getOverCount(this.bodyListObj[this.cenShu], t.length);
    };
    t.prototype.getRand = function (t) {
        return Math.floor(Math.random() * t * 100) / 100;
    };
    t.prototype.saveData = function () {
        cc.sys.localStorage.setItem("ai001", JSON.stringify(this.allWeight));
        cc.sys.localStorage.setItem("rightCount", this.rightCount);
        cc.sys.localStorage.setItem("errorCount", this.errorCount);
    };
    t.prototype.loadData = function () {
        this.allWeight = this.getAllObj("ai001");
        this.rightCount = this.getNumber("rightCount");
        this.errorCount = this.getNumber("errorCount");
    };
    t.prototype.getNumber = function () {
        var t = cc.sys.localStorage.getItem("rightCount");
        return t ? parseInt(t) : 0;
    };
    t.prototype.getAllObj = function (t) {
        var e = cc.sys.localStorage.getItem(t);
        return e ? JSON.parse(e) : {};
    };
    return t;
})();
o.default = a;
